package com.hsnn.medstgmini.pro.plan.dao.impl;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.hsnn.medstgmini.base.dict.dao.impl.BaseHibernateDAO;
import com.hsnn.medstgmini.dao.HibernateSqlUtil;
import com.hsnn.medstgmini.pro.plan.dao.PricePlanDao;
import com.hsnn.medstgmini.pro.plan.model.BasePriceadjplan;
import com.hsnn.medstgmini.util.DateUtil;
import com.hsnn.medstgmini.util.Pagination;

@Repository
public class PricePlanDaoImpl extends BaseHibernateDAO implements PricePlanDao {
	
	private static final Logger log = Logger.getLogger(PricePlanDaoImpl.class);
	
	@Autowired
	private HibernateSqlUtil dao;
	
	/**
	 * @category 校验计划名称是否相同
	 * @date 2015年7月2日
	 * @param name
	 * @return 存在相同计划名称返回true
	 */
	@SuppressWarnings("unchecked")
	public boolean checkSamePlanName(BasePriceadjplan plan) {
		Session session = getSession();
		if (plan != null) {
			String name = plan.getPriceAdjPlanName();
			if (StringUtils.isNotBlank(name)) {
				name = name.trim();
				Criteria criteria = session
						.createCriteria(BasePriceadjplan.class);
				criteria.add(Restrictions.eq("priceAdjPlanName", name));
				String id = plan.getPriceAdjPlanId();
				if (StringUtils.isNotBlank(id)) {
					id = id.trim();
					criteria.add(Restrictions.not(Restrictions.eq(
							"priceAdjPlanId", id)));
				}
				List<BasePriceadjplan> list = criteria.list();
				if (list != null && !list.isEmpty()) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * @category 根据条件查询商品目录
	 * @author 陈镜宇
	 * @date 2015年6月15日
	 * @param @param baseDelrelation
	 * @param @return
	 */
	@Override
	public void submitPricePlanAdd(BasePriceadjplan basePriceadjplan) {
		Session session = getSession();
		session.save(basePriceadjplan);
		session.flush();
	}

	/**
	 * @category 提交编辑调价计划
	 * @date 2015年7月5日
	 * @param bp
	 */
	public void submitPricePlanEdit(BasePriceadjplan bp) {
		Session session = getSession();
		if (bp != null) {
			String id = bp.getPriceAdjPlanId();
			BasePriceadjplan plan = getBidHospplanById(id);
			if (plan != null) {
				plan.setPriceAdjPlanName(bp.getPriceAdjPlanName());
				plan.setExecuteCat(bp.getExecuteCat());
				plan.setExecuteDatetime(bp.getExecuteDatetime());
				plan.setPriceAdjAccord(bp.getPriceAdjAccord());
				plan.setRemark(bp.getRemark());
				plan.setUpdDatetime(DateUtil.getTimeStamp());
				plan.setUpdUser(bp.getUpdUser());
				session.update(plan);
				session.flush();
			}
		}
	}

	@SuppressWarnings("rawtypes")
	public List findByProperty(String propertyName, Object value) {
		String queryString = "from BasePriceadjplan as model where model."
				+ propertyName + "= ?";
		Query queryObject = getSession().createQuery(queryString);
		queryObject.setParameter(0, value);
		return queryObject.list();
	}

	@Override
	public BasePriceadjplan getBidHospplanById(String id) {
		String queryString = "from BasePriceadjplan where priceAdjPlanId=:id ";
		Query queryObject = getSession().createQuery(queryString);
		queryObject.setString("id", id);
		BasePriceadjplan bidHospplan = (BasePriceadjplan) queryObject
				.uniqueResult();
		return bidHospplan;
	}
	
	/**
	 * 
	 * @category 根据id获取调价计划
	 * @auth wangbing
	 * @return Map
	 * @date 2015年6月23日
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Map<String,Object> getHospplanById(String id) {
		StringBuffer sql = new StringBuffer()
		.append("	select a.price_adj_plan_id priceAdjPlanId,a.price_adj_plan_name priceAdjPlanName,a.price_adj_plan_cat priceAdjPlanCat,")
		.append("	a.execute_cat executeCat,date_format(a.create_datetime,'%Y-%m-%d %H:%i:%S') createDatetime,b.name createUser,")
		.append("	date_format(a.execute_datetime,'%Y-%m-%d %H:%i:%S') executeDatetime,a.price_adj_accord priceAdjAccord,")
		.append("	a.remark remark,a.status status,date_format(a.upd_datetime,'%Y-%m-%d %H:%i:%S') updDatetime,c.name updUser ")
		.append("	from base_priceadjplan a left join sys_user b on b.id = a.create_user left join sys_user c on c.id = a.upd_user ")
		.append("	where a.price_adj_plan_id = :id ");
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("id", id);
		return (Map<String, Object>) dao.getSqlMapUniqueResult(sql.toString(), params);
	}
	
	@Override
	public int batchSubmitDrugTOPlan(List<Map<String,String>> list) {
		int sum = 0;
		for(Map<String,String> map:list){
			int count = addOneDetail(map);
			sum += count;
		}
		return sum;
	}
	
	/**
	 * 
	 * @category 加入一个调价计划
	 * @auth wangbing
	 * @return Pagination
	 * @date 2015年6月23日
	 */
	private int addOneDetail(Map<String,String> map){
		Session session = super.getSession();
		StringBuffer sql = new StringBuffer()
		.append("insert into base_priceadjplandetail values (?,?,?,?,?,?,?,?,?)");
		Query query = session.createSQLQuery(sql.toString());
		query.setParameter(0, map.get("priceAdjPalnDetailId"));
		query.setParameter(1, map.get("priceAdjPlanId"));
		query.setParameter(2, map.get("goodsCode"));
		query.setParameter(3, map.get("origProPriceLimit"));
		query.setParameter(4, map.get("currProPriceLimit"));
		query.setParameter(5, map.get("origRetailPriceLimit"));
		query.setParameter(6, map.get("currRetailPriceLlimit"));
		query.setParameter(7, map.get("addDatetime"));
		query.setParameter(8, map.get("addUser"));
		int count = query.executeUpdate();
		session.flush();
		return count;
	}
	
	/**
	 * 
	 * @category 获取调价计划明细列表
	 * @auth wangbing
	 * @return Pagination
	 * @date 2015年6月23日
	 */
	@Override
	public Pagination getPriceDurgList(Pagination page) {
		String drugComp = page.getConditions().get("drugComp");
		String projName = page.getConditions().get("projName");
		String goodsCode = page.getConditions().get("goodsCode");
		String drugName = page.getConditions().get("drugName");
		String drugCode = page.getConditions().get("drugCode");
		String priceid = page.getConditions().get("priceid");
		StringBuffer sql = new StringBuffer()
		.append("	SELECT a.price_adj_paln_detail_id priceAdjPalnDetailId,a.price_adj_plan_id priceAdjPlanId,")
		.append("	b.proj_name name,b.drug_code drugCode,b.qualityspec_name drugName,b.form_name formName,")
		.append("	b.factor factor,b.unit_name unit,b.prod_compname compName,b.material_name materialName,b.prod_compcode prodCompId,")
		.append("	a.orig_pro_price_limit proPriceLimit,a.curr_pro_price_limit currProPriceLimit,b.drug_code drugId,")
		.append("	a.orig_retail_price_limit retailPriceLimit,a.curr_retail_price_llimit currRetailPriceLlimit,")
		.append("	a.add_datetime addDatetime,a.goods_code goodsCode,b.spec_name specName")
		.append("	FROM base_priceadjplandetail a LEFT JOIN base_goods_item  b ON b.goods_code = a.goods_code ")
		.append("	WHERE a.price_adj_plan_id = :priceid ");
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("priceid", priceid);
		if(StringUtils.isNotBlank(drugComp)){
			sql.append(" and (b.prod_compname like :drugComp or b.prod_compname_spel like :drugComp)");
			params.put("drugComp", "%"+drugComp+"%");
		}
		if(StringUtils.isNotBlank(projName)){
			sql.append(" and b.proj_name like :projName ");
			params.put("projName", "%"+projName+"%");
		}
		if(StringUtils.isNotBlank(goodsCode)){
			sql.append(" and a.goods_code = :goodsCode ");
			params.put("goodsCode", goodsCode);
		}
		if(StringUtils.isNotBlank(drugName)){
			sql.append(" and (b.qualityspec_name like :drugName or b.qualityspec_name_spel like :drugName) ");
			params.put("drugName", "%"+drugName+"%");
		}
		if(StringUtils.isNotBlank(drugCode)){
			sql.append(" and b.drug_code = :drugCode ");
			params.put("drugCode", drugCode);
		}

		return dao.getPageMapList(sql.toString(), params, page);
	}


	@Override
	public int batchSubmitPlanDrugEdit(List<Map<String,String>> list) {
		int sum = 0;
		for(Map<String,String> map:list){
			int count = saveOneDetail(map);
			sum += count;
		}
		return sum;
	}
	
	/**
	 * 
	 * @category 保存一个调价计划明细
	 * @auth wangbing
	 * @return Pagination
	 * @date 2015年6月23日
	 */
	private int saveOneDetail(Map<String,String> map){
		Session session = super.getSession();
		StringBuffer sql = new StringBuffer()
		.append(" update base_priceadjplandetail set curr_pro_price_limit = :currProPriceLimit,")
		.append(" curr_retail_price_llimit = :currRetailPriceLlimit where price_adj_paln_detail_id = :priceAdjPalnDetailId ");
		Query query = session.createSQLQuery(sql.toString());
		query.setString("currProPriceLimit", map.get("currProPriceLimit"));
		query.setString("currRetailPriceLlimit", map.get("currRetailPriceLlimit"));
		query.setString("priceAdjPalnDetailId", map.get("priceAdjPalnDetailId"));
		int count = query.executeUpdate();
		return count;
	}
	
	
	/**
	 * 
	 * @category 获取未调价计划列表
	 * @auth wangbing
	 * @return
	 * @date 2015年6月23日
	 */
	@Override
	public Pagination getUnChangePlan(Pagination page, BasePriceadjplan plan) {
		String createStartTime = page.getConditions().get("createStartTime");
		String createEndTime = page.getConditions().get("createEndTime");
		String executeStartTime = page.getConditions().get("executeStartTime");
		String executeEndTime = page.getConditions().get("executeEndTime");
		String executeCat = page.getConditions().get("executeCat");
		String priceAdjPlanName = plan.getPriceAdjPlanName();
		String orgId = page.getConditions().get("orgId");
		StringBuffer sql = new StringBuffer()
		.append("	select a.price_adj_plan_id priceAdjPlanId,a.price_adj_plan_name priceAdjPlanName,a.price_adj_plan_cat priceAdjPlanCat,")
		.append("	a.execute_cat executeCat,date_format(a.create_datetime,'%Y-%m-%d %H:%i:%S') createDatetime,b.name createUser,")
		.append("	date_format(a.execute_datetime,'%Y-%m-%d %H:%i:%S') executeDatetime,a.price_adj_accord priceAdjAccord,")
		.append("	a.remark remark,a.status status,date_format(a.upd_datetime,'%Y-%m-%d %H:%i:%S') updDatetime,c.name updUser ")
		.append("	from base_priceadjplan a left join sys_user b on b.id = a.create_user left join sys_user c on c.id = a.upd_user ")
		.append("	where a.status = 0 and a.create_org_id = :orgId ");
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("orgId", orgId);
		if(StringUtils.isNotBlank(createStartTime)){
			sql.append(" and a.create_datetime >= :createStartTime ");
			params.put("createStartTime", createStartTime + " 00:00:00");
		}
		if(StringUtils.isNotBlank(createEndTime)){
			sql.append(" and a.create_datetime <= :createEndTime ");
			params.put("createEndTime", createEndTime + " 23:59:59");
		}
		if(StringUtils.isNotBlank(executeStartTime)){
			sql.append(" and a.execute_datetime >= :executeStartTime ");
			params.put("executeStartTime", executeStartTime + " 00:00:00");
		}
		if(StringUtils.isNotBlank(executeEndTime)){
			sql.append(" and a.execute_datetime <= :executeEndTime ");
			params.put("executeEndTime", executeEndTime + " 23:59:59");
		}
		if(StringUtils.isNotBlank(executeCat)){
			sql.append(" and a.execute_cat = :executeCat ");
			params.put("executeCat", executeCat);
		}
		if(StringUtils.isNotBlank(priceAdjPlanName)){
			sql.append(" and a.price_adj_plan_name like :priceAdjPlanName ");
			params.put("priceAdjPlanName", "%"+priceAdjPlanName+"%");
		}
		sql.append(" order by a.execute_datetime desc ");
		
		return dao.getPageMapList(sql.toString(), params, page);
	}

	/**
	 * @category 执行调价计划
	 * @auth 单大伟
	 * @date 2015年6月24日
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void runChangePlan(Pagination page) {
		String planId = page.getConditions().get("planId");
		String userId = page.getConditions().get("userId");
		String updDatetime = DateUtil.dateTime(new Timestamp(System.currentTimeMillis()));
		Session session = getSession();
		if (StringUtils.isNotBlank(planId)) {
			// 查询计划下明细列表
			StringBuffer detailSql = new StringBuffer()
			.append(" select goods_code goodsCode,curr_pro_price_limit currProPriceLimit,curr_retail_price_llimit currRetailPriceLlimit ")
			.append(" from base_priceadjplandetail where price_adj_plan_id = :priceAdjPlanId ");
			Map<String,Object> detailParams = new HashMap<String,Object>();
			detailParams.put("priceAdjPlanId", planId);
			List<Map<String,Object>> list = (List<Map<String, Object>>) dao.getSqlMapList(detailSql.toString(), detailParams);
			if (list != null && !list.isEmpty()) {
				for (Map<String,Object> detail : list) {
					// 最高采购限价
					// 更新商品表价格
					StringBuffer sb = new StringBuffer();
					sb.append("update base_goods_item ");
					sb.append("set pro_price_limit=:proPriceLimit, ");
					sb.append(" retail_price_limit=:retailPriceLimit, ");
					sb.append(" upd_datetime=:updDatetime, ");
					sb.append(" upd_user=:updUser ");
					sb.append("where goods_code=:goodsCode and pro_price_limit > :proPriceLimit and retail_price_limit > :retailPriceLimit ");
					SQLQuery query = session.createSQLQuery(sb.toString());
					query.setBigDecimal("proPriceLimit", new BigDecimal(detail.get("currProPriceLimit").toString()));
					query.setBigDecimal("retailPriceLimit",new BigDecimal(detail.get("currRetailPriceLlimit").toString()));
					query.setString("updDatetime", updDatetime);
					query.setString("updUser", userId);
					query.setString("goodsCode", detail.get("goodsCode").toString());
					query.executeUpdate();
					session.flush();
					// 插入商品表日志
					String sql = "INSERT INTO base_goods_item_log SELECT :logId log_id, a.*,null detail FROM base_goods_item a WHERE a.goods_code = :goodsCode";
					query = session.createSQLQuery(sql);
					query.setString("logId", UUID.randomUUID().toString());
					query.setString("goodsCode", detail.get("goodsCode").toString());
					query.executeUpdate();
					session.flush();

					// 更新购物计划(采购价不能超过最高采购限价)
					StringBuffer purPlanSql = new StringBuffer()
					.append("update pur_purplan set pro_price = :proPriceLimit,upd_datetime = :updDatetime,upd_user = :userId ")
					.append(" where pro_price > :proPriceLimit and goods_code = :goodsCode ");
					Query purPlanQuery = session.createSQLQuery(purPlanSql.toString());
					purPlanQuery.setBigDecimal("proPriceLimit", new BigDecimal(detail.get("currProPriceLimit").toString()));
					purPlanQuery.setString("updDatetime", updDatetime);
					purPlanQuery.setString("userId", userId);
					purPlanQuery.setString("goodsCode", detail.get("goodsCode").toString());
					purPlanQuery.executeUpdate();
					session.flush();
					

					// 更新医疗机构勾选的商品目录(采购价不能超过最高采购限价)
					StringBuffer getIdSql = new StringBuffer()
					.append("select hosp_pro_cat_id from pur_hospcatalog where pro_price > :proPriceLimit and goods_code = :goodsCode ");
					Query getIdQuery = session.createSQLQuery(getIdSql.toString());
					getIdQuery.setBigDecimal("proPriceLimit", new BigDecimal(detail.get("currProPriceLimit").toString()));
					getIdQuery.setString("goodsCode", detail.get("goodsCode").toString());
					List<String> idList = getIdQuery.list();
					
					StringBuffer hospSql = new StringBuffer()
					.append("update pur_hospcatalog set pro_price = :proPriceLimit,upd_datetime = :updDatetime,upd_user = :userId ")
					.append(" where pro_price > :proPriceLimit and goods_code = :goodsCode ");
					Query hospQuery = session.createSQLQuery(hospSql.toString());
					hospQuery.setBigDecimal("proPriceLimit", new BigDecimal(detail.get("currProPriceLimit").toString()));
					hospQuery.setString("updDatetime", updDatetime);
					hospQuery.setString("userId", userId);
					hospQuery.setString("goodsCode", detail.get("goodsCode").toString());
					hospQuery.executeUpdate();
					session.flush();
					
					//写日志
					for(String hospProCatId : idList){
						StringBuffer logSql = new StringBuffer()
						.append("INSERT INTO pur_hospcatalog_log SELECT :logId log_id, a.hosp_pro_cat_id, a.hosp_code, a.goods_code, ")
						.append("a.del_comp_code, a.pro_price, a.pro_count, a.status, a.bak_pur_count, 1 upd_type, a.upd_datetime, ")
						.append("a.upd_user FROM pur_hospcatalog a WHERE a.hosp_pro_cat_id = :hospProCatId ");
						Query logQuery = session.createSQLQuery(logSql.toString());
						logQuery.setString("logId", UUID.randomUUID().toString());
						logQuery.setString("hospProCatId", hospProCatId);
						logQuery.executeUpdate();
						session.flush();
					}
					
					
				}
			}else{
				log.warn("调价计划中没有设置明细!");
			}

			// 更新计划表状态为已处理
			StringBuffer sb = new StringBuffer();
			sb.append("update base_priceadjplan set ");
			sb.append("status=1 ");
			sb.append(", upd_datetime=:updDatetime ");
			sb.append(", upd_user=:updUser ");
			sb.append(", execute_datetime=:executeDatetime ");
			sb.append("where price_adj_plan_id=:priceAdjPlanId ");
			SQLQuery query = session.createSQLQuery(sb.toString());
			query.setString("updDatetime", updDatetime);
			query.setString("updUser", userId);
			query.setParameter("executeDatetime", DateUtil.parseDateHHTime(updDatetime));
			query.setString("priceAdjPlanId", planId);
			query.executeUpdate();
			session.flush();
		}
	}

	/**
	 * @category 批量执行定时的调价计划
	 * @author 陈镜宇
	 * @date 2015年6月15日
	 * @param @param baseDelrelation
	 * @param @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void runChangePlan() {
		Session session = getSession();
		String updDatetime = DateUtil.dateTime(new Timestamp(System.currentTimeMillis()));
		StringBuffer psql = new StringBuffer()
		.append("select price_adj_plan_id from base_priceadjplan where execute_cat = 2 and status = 0 and execute_datetime <= :newdate ");
		Query pquery = session.createSQLQuery(psql.toString());
		pquery.setString("newdate", DateUtil.dateTime(new Timestamp(System.currentTimeMillis())));
		//获取当前时间内自动执行的调价计划
		List<String> plist = pquery.list();
		if(plist != null && plist.size() > 0){
			for(String planId : plist){
				if (StringUtils.isNotBlank(planId)) {
					// 查询计划下明细列表
					StringBuffer detailSql = new StringBuffer()
					.append(" select goods_code goodsCode,curr_pro_price_limit currProPriceLimit,curr_retail_price_llimit currRetailPriceLlimit ")
					.append(" from base_priceadjplandetail where price_adj_plan_id = :priceAdjPlanId ");
					Map<String,Object> detailParams = new HashMap<String,Object>();
					detailParams.put("priceAdjPlanId", planId);
					List<Map<String,Object>> list = (List<Map<String, Object>>) dao.getSqlMapList(detailSql.toString(), detailParams);
					if (list != null && !list.isEmpty()) {
						for (Map<String,Object> detail : list) {
							// 最高采购限价
							// 更新商品表价格
							StringBuffer sb = new StringBuffer();
							sb.append("update base_goods_item ");
							sb.append("set pro_price_limit=:proPriceLimit, ");
							sb.append(" retail_price_limit=:retailPriceLimit, ");
							sb.append(" upd_datetime=:updDatetime ");
							sb.append("where goods_code=:goodsCode and pro_price_limit > :proPriceLimit and retail_price_limit > :retailPriceLimit ");
							SQLQuery query = session.createSQLQuery(sb.toString());
							query.setBigDecimal("proPriceLimit", new BigDecimal(detail.get("currProPriceLimit").toString()));
							query.setBigDecimal("retailPriceLimit",new BigDecimal(detail.get("currRetailPriceLlimit").toString()));
							query.setString("updDatetime", updDatetime);
							query.setString("goodsCode", detail.get("goodsCode").toString());
							query.executeUpdate();
							session.flush();
							// 插入商品表日志
							String sql = "INSERT INTO base_goods_item_log SELECT :logId log_id, a.*,null detail FROM base_goods_item a WHERE a.goods_code = :goodsCode";
							query = session.createSQLQuery(sql);
							query.setString("logId", UUID.randomUUID().toString());
							query.setString("goodsCode", detail.get("goodsCode").toString());
							query.executeUpdate();
							session.flush();

							// 更新购物计划(采购价不能超过最高采购限价)
							StringBuffer purPlanSql = new StringBuffer()
							.append("update pur_purplan set pro_price = :proPriceLimit,upd_datetime = :updDatetime ")
							.append(" where pro_price > :proPriceLimit and goods_code = :goodsCode ");
							Query purPlanQuery = session.createSQLQuery(purPlanSql.toString());
							purPlanQuery.setBigDecimal("proPriceLimit", new BigDecimal(detail.get("currProPriceLimit").toString()));
							purPlanQuery.setString("updDatetime", updDatetime);
							purPlanQuery.setString("goodsCode", detail.get("goodsCode").toString());
							purPlanQuery.executeUpdate();
							session.flush();
							

							// 更新医疗机构勾选的商品目录(采购价不能超过最高采购限价)
							StringBuffer getIdSql = new StringBuffer()
							.append("select hosp_pro_cat_id from pur_hospcatalog where pro_price > :proPriceLimit and goods_code = :goodsCode ");
							Query getIdQuery = session.createSQLQuery(getIdSql.toString());
							getIdQuery.setBigDecimal("proPriceLimit", new BigDecimal(detail.get("currProPriceLimit").toString()));
							getIdQuery.setString("goodsCode", detail.get("goodsCode").toString());
							List<String> idList = getIdQuery.list();
							
							StringBuffer hospSql = new StringBuffer()
							.append("update pur_hospcatalog set pro_price = :proPriceLimit,upd_datetime = :updDatetime ")
							.append(" where pro_price > :proPriceLimit and goods_code = :goodsCode ");
							Query hospQuery = session.createSQLQuery(hospSql.toString());
							hospQuery.setBigDecimal("proPriceLimit", new BigDecimal(detail.get("currProPriceLimit").toString()));
							hospQuery.setString("updDatetime", updDatetime);
							hospQuery.setString("goodsCode", detail.get("goodsCode").toString());
							hospQuery.executeUpdate();
							session.flush();
							
							//写日志
							for(String hospProCatId : idList){
								StringBuffer logSql = new StringBuffer()
								.append("INSERT INTO pur_hospcatalog_log SELECT :logId log_id, a.hosp_pro_cat_id, a.hosp_code, a.goods_code, ")
								.append("a.del_comp_code, a.pro_price, a.pro_count, a.status, a.bak_pur_count, 1 upd_type, a.upd_datetime, ")
								.append("a.upd_user FROM pur_hospcatalog a WHERE a.hosp_pro_cat_id = :hospProCatId ");
								Query logQuery = session.createSQLQuery(logSql.toString());
								logQuery.setString("logId", UUID.randomUUID().toString());
								logQuery.setString("hospProCatId", hospProCatId);
								logQuery.executeUpdate();
								session.flush();
							}
							
							
						}
					}else{
						log.warn("调价计划中没有设置明细!");
					}

					// 更新计划表状态为已处理
					StringBuffer sb = new StringBuffer();
					sb.append("update base_priceadjplan set ");
					sb.append("status=1 ");
					sb.append(", upd_datetime=:updDatetime ");
					sb.append("where price_adj_plan_id=:priceAdjPlanId ");
					SQLQuery query = session.createSQLQuery(sb.toString());
					query.setString("updDatetime", updDatetime);
					query.setString("priceAdjPlanId", planId);
					query.executeUpdate();
					session.flush();
				}

			}
		}else{
			log.warn("没有自动执行的调价计划!");
		}
		
	}
	
	/**
	 * 
	 * @category 删除调价计划明细
	 * @auth wangbing
	 * @return Pagination
	 * @date 2015年6月23日
	 */
	@Override
	public int batchDeleteDrug(List<String> list){
		Session session = super.getSession();
		StringBuffer sql = new StringBuffer()
		.append("delete from base_priceadjplandetail where price_adj_paln_detail_id in (:list)");
		Query query = session.createSQLQuery(sql.toString());
		query.setParameterList("list", list);
		int sum = query.executeUpdate();
		session.flush();
		return sum;
	}

}
